package org.garbagecan.dwrstudy.jsonlib;

import org.garbagecan.dwrstudy.*;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class JSONLib {

	public JSONObject getJSONFromMap() {
		Map map = new HashMap();
		map.put("name", "json");
		map.put("bool", Boolean.TRUE);
		map.put("int", new Integer(1));
		map.put("arr", new String[]{"a", "b"});
		map.put("func", "function(i){ return this.arr[i]; }");

		JSONObject jsonObject = JSONObject.fromObject(map);
		System.out.println(jsonObject);
		return jsonObject;
	}

	public JSONObject getJSONFromBean() {
		User user = new User();
		user.setName("kongxx");
		user.setPassword("wahaha");
		JSONObject jsonObject = JSONObject.fromObject(user);
		System.out.println(jsonObject);
		return jsonObject;
	}

	public JSONObject test() {
		MyBean mybean1 = new MyBean();
		mybean1.setId("Citi_Root");
		mybean1.setName("Citi Group");
		mybean1.setData(new MyData[] {new MyData("key1","1000"), new MyData("key2","100")});
		
		MyBean mybean12 = new MyBean();
		mybean12.setId("Root_SW");
		mybean12.setName("SW");
		mybean12.setData(new MyData[] {new MyData("key1","100"), new MyData("key2","10")});
		
		MyBean mybean13 = new MyBean();
		mybean13.setId("Root_HK");
		mybean13.setName("HK");
		mybean13.setData(new MyData[] {new MyData("key1","900"), new MyData("key2","90")});
		
		mybean1.setChildren(new MyBean[] {mybean12, mybean13});
		
		JSONObject jsonObject = JSONObject.fromObject(mybean1);
		System.out.println(jsonObject);
		return jsonObject;
	}
}